<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css" integrity="sha256-CTSx/A06dm1B063156EVh15m6Y67pAjZZaQc89LLSrU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.24/fancybox/fancybox.css" integrity="sha256-vQkngPS8jiHHH0I6ABTZroZk8NPZ7b+MUReOFE9UsXQ=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"rjyblog.gitee.io","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.18.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":null},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="okhttp源码详细解析，包含整体架构，以及各个拦截器的详解">
<meta property="og:type" content="article">
<meta property="og:title" content="okhttp详解系列一：开篇">
<meta property="og:url" content="https://rjyblog.gitee.io/posts/78efaca4.html">
<meta property="og:site_name" content="任建勇的博客">
<meta property="og:description" content="okhttp源码详细解析，包含整体架构，以及各个拦截器的详解">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-08-17T07:06:50.000Z">
<meta property="article:modified_time" content="2024-01-04T11:26:29.685Z">
<meta property="article:author" content="Jason">
<meta property="article:tag" content="okhttp">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://rjyblog.gitee.io/posts/78efaca4.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://rjyblog.gitee.io/posts/78efaca4.html","path":"posts/78efaca4.html","title":"okhttp详解系列一：开篇"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>okhttp详解系列一：开篇 | 任建勇的博客</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">任建勇的博客</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#okhttp-%E6%95%B4%E4%BD%93%E6%9E%B6%E6%9E%84"><span class="nav-text">okhttp 整体架构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8B%A6%E6%88%AA%E5%99%A8%E7%9A%84%E5%A4%84%E7%90%86%E6%97%B6%E5%BA%8F"><span class="nav-text">拦截器的处理时序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%90%8C%E6%AD%A5%E5%92%8C%E5%BC%82%E6%AD%A5%E8%AF%B7%E6%B1%82%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90"><span class="nav-text">同步和异步请求源码解析</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#okhttp%E5%BC%82%E6%AD%A5%E8%AF%B7%E6%B1%82%E8%B0%83%E7%94%A8"><span class="nav-text">okhttp异步请求调用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%90%8C%E6%AD%A5%E8%AF%B7%E6%B1%82%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90"><span class="nav-text">同步请求源码解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97"><span class="nav-text">使用指南</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BC%96%E8%AF%91okhttp-4-x"><span class="nav-text">编译okhttp 4.x</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="nav-text">参考资料</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Jason"
      src="/images/avatar.png">
  <p class="site-author-name" itemprop="name">Jason</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">66</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">13</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://rjyblog.gitee.io/posts/78efaca4.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.png">
      <meta itemprop="name" content="Jason">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="任建勇的博客">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="okhttp详解系列一：开篇 | 任建勇的博客">
      <meta itemprop="description" content="okhttp源码详细解析，包含整体架构，以及各个拦截器的详解">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          okhttp详解系列一：开篇
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-08-17 15:06:50" itemprop="dateCreated datePublished" datetime="2023-08-17T15:06:50+08:00">2023-08-17</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2024-01-04 19:26:29" itemprop="dateModified" datetime="2024-01-04T19:26:29+08:00">2024-01-04</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/okhttp/" itemprop="url" rel="index"><span itemprop="name">okhttp</span></a>
        </span>
    </span>

  
</div>

            <div class="post-description">okhttp源码详细解析，包含整体架构，以及各个拦截器的详解</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><ul>
<li><a href="/posts/78efaca4.html" title="okhttp详解系列一：开篇">okhttp详解系列一：开篇</a></li>
<li><a href="/posts/8e83ed63.html" title="okhttp详解系列二：重试重定向拦截器">okhttp详解系列二：重试重定向拦截器</a></li>
<li><a href="/posts/6e2b1d41.html" title="okhttp详解系列三：桥拦截器 BridgeInterceptor">okhttp详解系列三：桥拦截器 BridgeInterceptor</a></li>
<li><a href="/posts/37107c2e.html" title="okhttp详解系列四：缓存拦截器">okhttp详解系列四：缓存拦截器</a></li>
<li><a href="/posts/ee27d764.html" title="okhttp详解系列五：连接拦截器 ConnectInterceptor">okhttp详解系列五：连接拦截器 ConnectInterceptor</a></li>
<li><a href="/posts/73cf04e1.html" title="okhttp详解系列六：服务请求拦截器 CallServerInterceptor">okhttp详解系列六：服务请求拦截器 CallServerInterceptor</a>

</li>
</ul>
<h2 id="okhttp-整体架构"><a href="#okhttp-整体架构" class="headerlink" title="okhttp 整体架构"></a>okhttp 整体架构</h2><?xml version="1.0" encoding="us-ascii" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentStyleType="text/css" height="435px" preserveAspectRatio="none" style="width:839px;height:435px;background:#FFFFFF;" version="1.1" viewBox="0 0 839 435" width="839px" zoomAndPan="magnify"><defs/><g><!--class RealCall--><g id="elem_RealCall"><rect fill="#F1F1F1" height="48" id="RealCall" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="89" x="407" y="7"/><ellipse cx="422" cy="23" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M424.3438,18.6719 C423.4063,18.2344 422.8125,18.0938 421.9375,18.0938 C419.3125,18.0938 417.3125,20.1719 417.3125,22.8906 L417.3125,24.0156 C417.3125,26.5938 419.4219,28.4844 422.3125,28.4844 C423.5313,28.4844 424.6875,28.1875 425.4375,27.6406 C426.0156,27.2344 426.3438,26.7813 426.3438,26.3906 C426.3438,25.9375 425.9531,25.5469 425.4844,25.5469 C425.2656,25.5469 425.0625,25.625 424.875,25.8125 C424.4219,26.2969 424.4219,26.2969 424.2344,26.3906 C423.8125,26.6563 423.125,26.7813 422.3594,26.7813 C420.3125,26.7813 419.0156,25.6875 419.0156,23.9844 L419.0156,22.8906 C419.0156,21.1094 420.2656,19.7969 422,19.7969 C422.5781,19.7969 423.1875,19.9531 423.6563,20.2031 C424.1406,20.4844 424.3125,20.7031 424.4063,21.1094 C424.4688,21.5156 424.5,21.6406 424.6406,21.7656 C424.7813,21.9063 425.0156,22.0156 425.2344,22.0156 C425.5,22.0156 425.7656,21.875 425.9375,21.6563 C426.0469,21.5 426.0781,21.3125 426.0781,20.8906 L426.0781,19.4688 C426.0781,19.0313 426.0625,18.9063 425.9688,18.75 C425.8125,18.4844 425.5313,18.3438 425.2344,18.3438 C424.9375,18.3438 424.7344,18.4375 424.5156,18.75 L424.3438,18.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="57" x="436" y="27.8467">RealCall</text><line style="stroke:#181818;stroke-width:0.5;" x1="408" x2="495" y1="39" y2="39"/><line style="stroke:#181818;stroke-width:0.5;" x1="408" x2="495" y1="47" y2="47"/></g><!--class OkHttpClient--><g id="elem_OkHttpClient"><rect fill="#F1F1F1" height="48" id="OkHttpClient" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="129" x="531" y="7"/><ellipse cx="546" cy="23" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M548.3438,18.6719 C547.4063,18.2344 546.8125,18.0938 545.9375,18.0938 C543.3125,18.0938 541.3125,20.1719 541.3125,22.8906 L541.3125,24.0156 C541.3125,26.5938 543.4219,28.4844 546.3125,28.4844 C547.5313,28.4844 548.6875,28.1875 549.4375,27.6406 C550.0156,27.2344 550.3438,26.7813 550.3438,26.3906 C550.3438,25.9375 549.9531,25.5469 549.4844,25.5469 C549.2656,25.5469 549.0625,25.625 548.875,25.8125 C548.4219,26.2969 548.4219,26.2969 548.2344,26.3906 C547.8125,26.6563 547.125,26.7813 546.3594,26.7813 C544.3125,26.7813 543.0156,25.6875 543.0156,23.9844 L543.0156,22.8906 C543.0156,21.1094 544.2656,19.7969 546,19.7969 C546.5781,19.7969 547.1875,19.9531 547.6563,20.2031 C548.1406,20.4844 548.3125,20.7031 548.4063,21.1094 C548.4688,21.5156 548.5,21.6406 548.6406,21.7656 C548.7813,21.9063 549.0156,22.0156 549.2344,22.0156 C549.5,22.0156 549.7656,21.875 549.9375,21.6563 C550.0469,21.5 550.0781,21.3125 550.0781,20.8906 L550.0781,19.4688 C550.0781,19.0313 550.0625,18.9063 549.9688,18.75 C549.8125,18.4844 549.5313,18.3438 549.2344,18.3438 C548.9375,18.3438 548.7344,18.4375 548.5156,18.75 L548.3438,18.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="97" x="560" y="27.8467">OkHttpClient</text><line style="stroke:#181818;stroke-width:0.5;" x1="532" x2="659" y1="39" y2="39"/><line style="stroke:#181818;stroke-width:0.5;" x1="532" x2="659" y1="47" y2="47"/></g><!--class RealInterceptorChain--><g id="elem_RealInterceptorChain"><rect codeLine="10" fill="#F1F1F1" height="96.8906" id="RealInterceptorChain" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="244" x="329.5" y="115"/><ellipse cx="370.15" cy="131" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M372.4938,126.6719 C371.5563,126.2344 370.9625,126.0938 370.0875,126.0938 C367.4625,126.0938 365.4625,128.1719 365.4625,130.8906 L365.4625,132.0156 C365.4625,134.5938 367.5719,136.4844 370.4625,136.4844 C371.6813,136.4844 372.8375,136.1875 373.5875,135.6406 C374.1656,135.2344 374.4938,134.7813 374.4938,134.3906 C374.4938,133.9375 374.1031,133.5469 373.6344,133.5469 C373.4156,133.5469 373.2125,133.625 373.025,133.8125 C372.5719,134.2969 372.5719,134.2969 372.3844,134.3906 C371.9625,134.6563 371.275,134.7813 370.5094,134.7813 C368.4625,134.7813 367.1656,133.6875 367.1656,131.9844 L367.1656,130.8906 C367.1656,129.1094 368.4156,127.7969 370.15,127.7969 C370.7281,127.7969 371.3375,127.9531 371.8063,128.2031 C372.2906,128.4844 372.4625,128.7031 372.5563,129.1094 C372.6188,129.5156 372.65,129.6406 372.7906,129.7656 C372.9313,129.9063 373.1656,130.0156 373.3844,130.0156 C373.65,130.0156 373.9156,129.875 374.0875,129.6563 C374.1969,129.5 374.2281,129.3125 374.2281,128.8906 L374.2281,127.4688 C374.2281,127.0313 374.2125,126.9063 374.1188,126.75 C373.9625,126.4844 373.6813,126.3438 373.3844,126.3438 C373.0875,126.3438 372.8844,126.4375 372.6656,126.75 L372.4938,126.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="155" x="389.85" y="135.8467">RealInterceptorChain</text><line style="stroke:#181818;stroke-width:0.5;" x1="330.5" x2="572.5" y1="147" y2="147"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="68" x="335.5" y="163.9951">index: Int</text><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="232" x="335.5" y="180.292">interceptors: List&lt;Interceptor&gt;</text><line style="stroke:#181818;stroke-width:0.5;" x1="330.5" x2="572.5" y1="187.5938" y2="187.5938"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="213" x="335.5" y="204.5889">Response proceed(Request r)</text></g><!--class Interceptor--><g id="elem_Interceptor"><rect codeLine="9" fill="#F1F1F1" height="48" id="Interceptor" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="111" x="231" y="272"/><ellipse cx="246" cy="288" fill="#B4A7E5" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M246.9531,284.7813 L248.6719,284.7813 C249.0625,284.7813 249.25,284.75 249.375,284.6719 C249.6406,284.5156 249.7813,284.2344 249.7813,283.9375 C249.7813,283.6719 249.6719,283.4063 249.4375,283.2344 C249.2656,283.125 249.125,283.0938 248.6719,283.0938 L243.5313,283.0938 C243.0938,283.0938 242.9688,283.1094 242.8125,283.2031 C242.5625,283.3594 242.4063,283.6563 242.4063,283.9375 C242.4063,284.2188 242.5469,284.4688 242.7656,284.6406 C242.9219,284.75 243.1094,284.7813 243.5313,284.7813 L245.25,284.7813 L245.25,291.2969 L243.5313,291.2969 C243.0938,291.2969 242.9688,291.3125 242.8125,291.4219 C242.5625,291.5781 242.4063,291.8594 242.4063,292.1563 C242.4063,292.4063 242.5469,292.6719 242.7656,292.8281 C242.9219,292.9531 243.125,293 243.5313,293 L248.6719,293 C249.4219,293 249.7813,292.7188 249.7813,292.1563 C249.7813,291.875 249.6719,291.625 249.4375,291.4531 C249.2656,291.3281 249.125,291.2969 248.6719,291.2969 L246.9531,291.2969 L246.9531,284.7813 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" font-style="italic" lengthAdjust="spacing" textLength="79" x="260" y="292.8467">Interceptor</text><line style="stroke:#181818;stroke-width:0.5;" x1="232" x2="341" y1="304" y2="304"/><line style="stroke:#181818;stroke-width:0.5;" x1="232" x2="341" y1="312" y2="312"/></g><!--class ExchangeFinder--><g id="elem_ExchangeFinder"><rect fill="#F1F1F1" height="48" id="ExchangeFinder" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="146" x="670.5" y="272"/><ellipse cx="685.5" cy="288" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M687.8438,283.6719 C686.9063,283.2344 686.3125,283.0938 685.4375,283.0938 C682.8125,283.0938 680.8125,285.1719 680.8125,287.8906 L680.8125,289.0156 C680.8125,291.5938 682.9219,293.4844 685.8125,293.4844 C687.0313,293.4844 688.1875,293.1875 688.9375,292.6406 C689.5156,292.2344 689.8438,291.7813 689.8438,291.3906 C689.8438,290.9375 689.4531,290.5469 688.9844,290.5469 C688.7656,290.5469 688.5625,290.625 688.375,290.8125 C687.9219,291.2969 687.9219,291.2969 687.7344,291.3906 C687.3125,291.6563 686.625,291.7813 685.8594,291.7813 C683.8125,291.7813 682.5156,290.6875 682.5156,288.9844 L682.5156,287.8906 C682.5156,286.1094 683.7656,284.7969 685.5,284.7969 C686.0781,284.7969 686.6875,284.9531 687.1563,285.2031 C687.6406,285.4844 687.8125,285.7031 687.9063,286.1094 C687.9688,286.5156 688,286.6406 688.1406,286.7656 C688.2813,286.9063 688.5156,287.0156 688.7344,287.0156 C689,287.0156 689.2656,286.875 689.4375,286.6563 C689.5469,286.5 689.5781,286.3125 689.5781,285.8906 L689.5781,284.4688 C689.5781,284.0313 689.5625,283.9063 689.4688,283.75 C689.3125,283.4844 689.0313,283.3438 688.7344,283.3438 C688.4375,283.3438 688.2344,283.4375 688.0156,283.75 L687.8438,283.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="114" x="699.5" y="292.8467">ExchangeFinder</text><line style="stroke:#181818;stroke-width:0.5;" x1="671.5" x2="815.5" y1="304" y2="304"/><line style="stroke:#181818;stroke-width:0.5;" x1="671.5" x2="815.5" y1="312" y2="312"/></g><!--class RealConnectionPool--><g id="elem_RealConnectionPool"><rect fill="#F1F1F1" height="48" id="RealConnectionPool" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="177" x="655" y="380"/><ellipse cx="670" cy="396" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M672.3438,391.6719 C671.4063,391.2344 670.8125,391.0938 669.9375,391.0938 C667.3125,391.0938 665.3125,393.1719 665.3125,395.8906 L665.3125,397.0156 C665.3125,399.5938 667.4219,401.4844 670.3125,401.4844 C671.5313,401.4844 672.6875,401.1875 673.4375,400.6406 C674.0156,400.2344 674.3438,399.7813 674.3438,399.3906 C674.3438,398.9375 673.9531,398.5469 673.4844,398.5469 C673.2656,398.5469 673.0625,398.625 672.875,398.8125 C672.4219,399.2969 672.4219,399.2969 672.2344,399.3906 C671.8125,399.6563 671.125,399.7813 670.3594,399.7813 C668.3125,399.7813 667.0156,398.6875 667.0156,396.9844 L667.0156,395.8906 C667.0156,394.1094 668.2656,392.7969 670,392.7969 C670.5781,392.7969 671.1875,392.9531 671.6563,393.2031 C672.1406,393.4844 672.3125,393.7031 672.4063,394.1094 C672.4688,394.5156 672.5,394.6406 672.6406,394.7656 C672.7813,394.9063 673.0156,395.0156 673.2344,395.0156 C673.5,395.0156 673.7656,394.875 673.9375,394.6563 C674.0469,394.5 674.0781,394.3125 674.0781,393.8906 L674.0781,392.4688 C674.0781,392.0313 674.0625,391.9063 673.9688,391.75 C673.8125,391.4844 673.5313,391.3438 673.2344,391.3438 C672.9375,391.3438 672.7344,391.4375 672.5156,391.75 L672.3438,391.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="145" x="684" y="400.8467">RealConnectionPool</text><line style="stroke:#181818;stroke-width:0.5;" x1="656" x2="831" y1="412" y2="412"/><line style="stroke:#181818;stroke-width:0.5;" x1="656" x2="831" y1="420" y2="420"/></g><!--class Exchange--><g id="elem_Exchange"><rect fill="#F1F1F1" height="48" id="Exchange" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="101" x="609" y="139.5"/><ellipse cx="624" cy="155.5" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M626.3438,151.1719 C625.4063,150.7344 624.8125,150.5938 623.9375,150.5938 C621.3125,150.5938 619.3125,152.6719 619.3125,155.3906 L619.3125,156.5156 C619.3125,159.0938 621.4219,160.9844 624.3125,160.9844 C625.5313,160.9844 626.6875,160.6875 627.4375,160.1406 C628.0156,159.7344 628.3438,159.2813 628.3438,158.8906 C628.3438,158.4375 627.9531,158.0469 627.4844,158.0469 C627.2656,158.0469 627.0625,158.125 626.875,158.3125 C626.4219,158.7969 626.4219,158.7969 626.2344,158.8906 C625.8125,159.1563 625.125,159.2813 624.3594,159.2813 C622.3125,159.2813 621.0156,158.1875 621.0156,156.4844 L621.0156,155.3906 C621.0156,153.6094 622.2656,152.2969 624,152.2969 C624.5781,152.2969 625.1875,152.4531 625.6563,152.7031 C626.1406,152.9844 626.3125,153.2031 626.4063,153.6094 C626.4688,154.0156 626.5,154.1406 626.6406,154.2656 C626.7813,154.4063 627.0156,154.5156 627.2344,154.5156 C627.5,154.5156 627.7656,154.375 627.9375,154.1563 C628.0469,154 628.0781,153.8125 628.0781,153.3906 L628.0781,151.9688 C628.0781,151.5313 628.0625,151.4063 627.9688,151.25 C627.8125,150.9844 627.5313,150.8438 627.2344,150.8438 C626.9375,150.8438 626.7344,150.9375 626.5156,151.25 L626.3438,151.1719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="69" x="638" y="160.3467">Exchange</text><line style="stroke:#181818;stroke-width:0.5;" x1="610" x2="709" y1="171.5" y2="171.5"/><line style="stroke:#181818;stroke-width:0.5;" x1="610" x2="709" y1="179.5" y2="179.5"/></g><!--class RetryAndFollowUpInterceptor--><g id="elem_RetryAndFollowUpInterceptor"><rect fill="#F1F1F1" height="48" id="RetryAndFollowUpInterceptor" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="248" x="377.5" y="272"/><ellipse cx="392.5" cy="288" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M394.8438,283.6719 C393.9063,283.2344 393.3125,283.0938 392.4375,283.0938 C389.8125,283.0938 387.8125,285.1719 387.8125,287.8906 L387.8125,289.0156 C387.8125,291.5938 389.9219,293.4844 392.8125,293.4844 C394.0313,293.4844 395.1875,293.1875 395.9375,292.6406 C396.5156,292.2344 396.8438,291.7813 396.8438,291.3906 C396.8438,290.9375 396.4531,290.5469 395.9844,290.5469 C395.7656,290.5469 395.5625,290.625 395.375,290.8125 C394.9219,291.2969 394.9219,291.2969 394.7344,291.3906 C394.3125,291.6563 393.625,291.7813 392.8594,291.7813 C390.8125,291.7813 389.5156,290.6875 389.5156,288.9844 L389.5156,287.8906 C389.5156,286.1094 390.7656,284.7969 392.5,284.7969 C393.0781,284.7969 393.6875,284.9531 394.1563,285.2031 C394.6406,285.4844 394.8125,285.7031 394.9063,286.1094 C394.9688,286.5156 395,286.6406 395.1406,286.7656 C395.2813,286.9063 395.5156,287.0156 395.7344,287.0156 C396,287.0156 396.2656,286.875 396.4375,286.6563 C396.5469,286.5 396.5781,286.3125 396.5781,285.8906 L396.5781,284.4688 C396.5781,284.0313 396.5625,283.9063 396.4688,283.75 C396.3125,283.4844 396.0313,283.3438 395.7344,283.3438 C395.4375,283.3438 395.2344,283.4375 395.0156,283.75 L394.8438,283.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="216" x="406.5" y="292.8467">RetryAndFollowUpInterceptor</text><line style="stroke:#181818;stroke-width:0.5;" x1="378.5" x2="624.5" y1="304" y2="304"/><line style="stroke:#181818;stroke-width:0.5;" x1="378.5" x2="624.5" y1="312" y2="312"/></g><!--class BridgeInterceptor--><g id="elem_BridgeInterceptor"><rect fill="#F1F1F1" height="48" id="BridgeInterceptor" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="163" x="8" y="380"/><ellipse cx="23" cy="396" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M25.3438,391.6719 C24.4063,391.2344 23.8125,391.0938 22.9375,391.0938 C20.3125,391.0938 18.3125,393.1719 18.3125,395.8906 L18.3125,397.0156 C18.3125,399.5938 20.4219,401.4844 23.3125,401.4844 C24.5313,401.4844 25.6875,401.1875 26.4375,400.6406 C27.0156,400.2344 27.3438,399.7813 27.3438,399.3906 C27.3438,398.9375 26.9531,398.5469 26.4844,398.5469 C26.2656,398.5469 26.0625,398.625 25.875,398.8125 C25.4219,399.2969 25.4219,399.2969 25.2344,399.3906 C24.8125,399.6563 24.125,399.7813 23.3594,399.7813 C21.3125,399.7813 20.0156,398.6875 20.0156,396.9844 L20.0156,395.8906 C20.0156,394.1094 21.2656,392.7969 23,392.7969 C23.5781,392.7969 24.1875,392.9531 24.6563,393.2031 C25.1406,393.4844 25.3125,393.7031 25.4063,394.1094 C25.4688,394.5156 25.5,394.6406 25.6406,394.7656 C25.7813,394.9063 26.0156,395.0156 26.2344,395.0156 C26.5,395.0156 26.7656,394.875 26.9375,394.6563 C27.0469,394.5 27.0781,394.3125 27.0781,393.8906 L27.0781,392.4688 C27.0781,392.0313 27.0625,391.9063 26.9688,391.75 C26.8125,391.4844 26.5313,391.3438 26.2344,391.3438 C25.9375,391.3438 25.7344,391.4375 25.5156,391.75 L25.3438,391.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="131" x="37" y="400.8467">BridgeInterceptor</text><line style="stroke:#181818;stroke-width:0.5;" x1="9" x2="170" y1="412" y2="412"/><line style="stroke:#181818;stroke-width:0.5;" x1="9" x2="170" y1="420" y2="420"/></g><!--class CacheInterceptor--><g id="elem_CacheInterceptor"><rect fill="#F1F1F1" height="48" id="CacheInterceptor" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="160" x="206.5" y="380"/><ellipse cx="221.5" cy="396" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M223.8438,391.6719 C222.9063,391.2344 222.3125,391.0938 221.4375,391.0938 C218.8125,391.0938 216.8125,393.1719 216.8125,395.8906 L216.8125,397.0156 C216.8125,399.5938 218.9219,401.4844 221.8125,401.4844 C223.0313,401.4844 224.1875,401.1875 224.9375,400.6406 C225.5156,400.2344 225.8438,399.7813 225.8438,399.3906 C225.8438,398.9375 225.4531,398.5469 224.9844,398.5469 C224.7656,398.5469 224.5625,398.625 224.375,398.8125 C223.9219,399.2969 223.9219,399.2969 223.7344,399.3906 C223.3125,399.6563 222.625,399.7813 221.8594,399.7813 C219.8125,399.7813 218.5156,398.6875 218.5156,396.9844 L218.5156,395.8906 C218.5156,394.1094 219.7656,392.7969 221.5,392.7969 C222.0781,392.7969 222.6875,392.9531 223.1563,393.2031 C223.6406,393.4844 223.8125,393.7031 223.9063,394.1094 C223.9688,394.5156 224,394.6406 224.1406,394.7656 C224.2813,394.9063 224.5156,395.0156 224.7344,395.0156 C225,395.0156 225.2656,394.875 225.4375,394.6563 C225.5469,394.5 225.5781,394.3125 225.5781,393.8906 L225.5781,392.4688 C225.5781,392.0313 225.5625,391.9063 225.4688,391.75 C225.3125,391.4844 225.0313,391.3438 224.7344,391.3438 C224.4375,391.3438 224.2344,391.4375 224.0156,391.75 L223.8438,391.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="128" x="235.5" y="400.8467">CacheInterceptor</text><line style="stroke:#181818;stroke-width:0.5;" x1="207.5" x2="365.5" y1="412" y2="412"/><line style="stroke:#181818;stroke-width:0.5;" x1="207.5" x2="365.5" y1="420" y2="420"/></g><!--class ConnectInterceptor--><g id="elem_ConnectInterceptor"><rect fill="#F1F1F1" height="48" id="ConnectInterceptor" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="177" x="402" y="380"/><ellipse cx="417" cy="396" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M419.3438,391.6719 C418.4063,391.2344 417.8125,391.0938 416.9375,391.0938 C414.3125,391.0938 412.3125,393.1719 412.3125,395.8906 L412.3125,397.0156 C412.3125,399.5938 414.4219,401.4844 417.3125,401.4844 C418.5313,401.4844 419.6875,401.1875 420.4375,400.6406 C421.0156,400.2344 421.3438,399.7813 421.3438,399.3906 C421.3438,398.9375 420.9531,398.5469 420.4844,398.5469 C420.2656,398.5469 420.0625,398.625 419.875,398.8125 C419.4219,399.2969 419.4219,399.2969 419.2344,399.3906 C418.8125,399.6563 418.125,399.7813 417.3594,399.7813 C415.3125,399.7813 414.0156,398.6875 414.0156,396.9844 L414.0156,395.8906 C414.0156,394.1094 415.2656,392.7969 417,392.7969 C417.5781,392.7969 418.1875,392.9531 418.6563,393.2031 C419.1406,393.4844 419.3125,393.7031 419.4063,394.1094 C419.4688,394.5156 419.5,394.6406 419.6406,394.7656 C419.7813,394.9063 420.0156,395.0156 420.2344,395.0156 C420.5,395.0156 420.7656,394.875 420.9375,394.6563 C421.0469,394.5 421.0781,394.3125 421.0781,393.8906 L421.0781,392.4688 C421.0781,392.0313 421.0625,391.9063 420.9688,391.75 C420.8125,391.4844 420.5313,391.3438 420.2344,391.3438 C419.9375,391.3438 419.7344,391.4375 419.5156,391.75 L419.3438,391.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="145" x="431" y="400.8467">ConnectInterceptor</text><line style="stroke:#181818;stroke-width:0.5;" x1="403" x2="578" y1="412" y2="412"/><line style="stroke:#181818;stroke-width:0.5;" x1="403" x2="578" y1="420" y2="420"/></g><!--class CallServerInterceptor--><g id="elem_CallServerInterceptor"><rect fill="#F1F1F1" height="48" id="CallServerInterceptor" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="189" x="7" y="272"/><ellipse cx="22" cy="288" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M24.3438,283.6719 C23.4063,283.2344 22.8125,283.0938 21.9375,283.0938 C19.3125,283.0938 17.3125,285.1719 17.3125,287.8906 L17.3125,289.0156 C17.3125,291.5938 19.4219,293.4844 22.3125,293.4844 C23.5313,293.4844 24.6875,293.1875 25.4375,292.6406 C26.0156,292.2344 26.3438,291.7813 26.3438,291.3906 C26.3438,290.9375 25.9531,290.5469 25.4844,290.5469 C25.2656,290.5469 25.0625,290.625 24.875,290.8125 C24.4219,291.2969 24.4219,291.2969 24.2344,291.3906 C23.8125,291.6563 23.125,291.7813 22.3594,291.7813 C20.3125,291.7813 19.0156,290.6875 19.0156,288.9844 L19.0156,287.8906 C19.0156,286.1094 20.2656,284.7969 22,284.7969 C22.5781,284.7969 23.1875,284.9531 23.6563,285.2031 C24.1406,285.4844 24.3125,285.7031 24.4063,286.1094 C24.4688,286.5156 24.5,286.6406 24.6406,286.7656 C24.7813,286.9063 25.0156,287.0156 25.2344,287.0156 C25.5,287.0156 25.7656,286.875 25.9375,286.6563 C26.0469,286.5 26.0781,286.3125 26.0781,285.8906 L26.0781,284.4688 C26.0781,284.0313 26.0625,283.9063 25.9688,283.75 C25.8125,283.4844 25.5313,283.3438 25.2344,283.3438 C24.9375,283.3438 24.7344,283.4375 24.5156,283.75 L24.3438,283.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="157" x="36" y="292.8467">CallServerInterceptor</text><line style="stroke:#181818;stroke-width:0.5;" x1="8" x2="195" y1="304" y2="304"/><line style="stroke:#181818;stroke-width:0.5;" x1="8" x2="195" y1="312" y2="312"/></g><!--reverse link RealCall to OkHttpClient--><g id="link_RealCall_OkHttpClient"><path codeLine="1" d="M508.219,31 C519.724,31 519.229,31 530.734,31 " fill="none" id="RealCall-backto-OkHttpClient" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="496.219,31,502.219,35,508.219,31,502.219,27,496.219,31" style="stroke:#181818;stroke-width:1.0;"/></g><!--link RealCall to RealInterceptorChain--><g id="link_RealCall_RealInterceptorChain"><path codeLine="2" d="M451.5,55.03 C451.5,71.567 451.5,88.444 451.5,108.843 " fill="none" id="RealCall-to-RealInterceptorChain" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="451.5,114.843,455.5,105.843,451.5,109.843,447.5,105.843,451.5,114.843" style="stroke:#181818;stroke-width:1.0;"/></g><!--link RealCall to Interceptor--><g id="link_RealCall_Interceptor"><path codeLine="3" d="M406.826,46.572 C374.936,59.398 333.874,81.354 311.5,115 C279.355,163.34 279.9831,229.2027 283.1711,265.7877 " fill="none" id="RealCall-to-Interceptor" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="283.692,271.765,286.8956,262.4517,283.2579,266.7839,278.9258,263.1462,283.692,271.765" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link RealCall to ExchangeFinder--><g id="link_RealCall_ExchangeFinder"><path codeLine="4" d="M507.35,53.0588 C513.194,55.1458 507.822,53.154 513.5,55 C607.44,85.537 664.392,39.009 727.5,115 C764.681,159.771 756.38,234.17 748.988,271.752 " fill="none" id="RealCall-backto-ExchangeFinder" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="496.049,49.023,500.3542,54.8079,507.35,53.0588,503.0448,47.2739,496.049,49.023" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link ExchangeFinder to RealConnectionPool--><g id="link_ExchangeFinder_RealConnectionPool"><path codeLine="5" d="M743.5,332 C743.5,349.6584 743.5,361.9408 743.5,379.6784 " fill="none" id="ExchangeFinder-backto-RealConnectionPool" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="743.5,320,739.5,326,743.5,332,747.5,326,743.5,320" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link Exchange to ExchangeFinder--><g id="link_Exchange_ExchangeFinder"><path codeLine="6" d="M680.8189,197.6204 C696.0789,221.3284 713.232,247.977 728.541,271.761 " fill="none" id="Exchange-backto-ExchangeFinder" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="674.324,187.53,674.2079,194.7402,680.8189,197.6204,680.9349,190.4103,674.324,187.53" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link RealInterceptorChain to Exchange--><g id="link_RealInterceptorChain_Exchange"><path codeLine="7" d="M585.781,163.5 C597.501,163.5 597.22,163.5 608.939,163.5 " fill="none" id="RealInterceptorChain-backto-Exchange" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="573.781,163.5,579.781,167.5,585.781,163.5,579.781,159.5,573.781,163.5" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link RealInterceptorChain to Interceptor--><g id="link_RealInterceptorChain_Interceptor"><path codeLine="15" d="M381.585,219.7961 C356.016,240.0191 336.818,255.203 316.008,271.662 " fill="none" id="RealInterceptorChain-backto-Interceptor" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="390.997,212.352,383.8096,212.9367,381.585,219.7961,388.7724,219.2114,390.997,212.352" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="8" x="374.3294" y="232.0075">1</text><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="8" x="314.2256" y="260.9846">n</text></g><!--reverse link Interceptor to RetryAndFollowUpInterceptor--><g id="link_Interceptor_RetryAndFollowUpInterceptor"><path codeLine="17" d="M360.35,296 C372.058,296 365.766,296 377.475,296 " fill="none" id="Interceptor-backto-RetryAndFollowUpInterceptor" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="342.35,296,360.35,302,360.35,290,342.35,296" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link Interceptor to BridgeInterceptor--><g id="link_Interceptor_BridgeInterceptor"><path codeLine="18" d="M227.9031,328.5292 C194.9731,346.2485 165.497,362.1083 132.502,379.8617 " fill="none" id="Interceptor-backto-BridgeInterceptor" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="243.754,320,225.06,323.2456,230.7461,333.8129,243.754,320" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link Interceptor to CacheInterceptor--><g id="link_Interceptor_CacheInterceptor"><path codeLine="19" d="M286.5,338 C286.5,355.6584 286.5,361.9408 286.5,379.6784 " fill="none" id="Interceptor-backto-CacheInterceptor" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="286.5,320,280.5,338,292.5,338,286.5,320" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link Interceptor to ConnectInterceptor--><g id="link_Interceptor_ConnectInterceptor"><path codeLine="20" d="M346.7373,328.2997 C380.8373,346.019 411.803,362.1083 445.97,379.8617 " fill="none" id="Interceptor-backto-ConnectInterceptor" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="330.765,320,343.9708,333.6238,349.5039,322.9756,330.765,320" style="stroke:#181818;stroke-width:1.0;"/></g><!--link CallServerInterceptor to Interceptor--><g id="link_CallServerInterceptor_Interceptor"><path codeLine="21" d="M196.168,296 C207.695,296 201.221,296 212.748,296 " fill="none" id="CallServerInterceptor-to-Interceptor" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="230.748,296,212.748,290,212.748,302,230.748,296" style="stroke:#181818;stroke-width:1.0;"/></g><!--SRC=[XP51ImCn48Nl-HN3dbJ8mMif1Lqi2WLbnHyGijDkS9YaoQWLzR-xMRLcozBczdZVlBn96ZHK6oAuq73_V2iIQdB8efe_G-i5P777Wj5Y41_hpZWU4uNxR_IPDplR6TxWqd6BKOrb3-WXk_RCQCLvVl2UzjJW7oHCLIalKifbTsqiZafPCYbDBW8V2lgJutUpFFChzrIQmSebcHU3Yu5fC0NF2I54Rn7RimQtBvW4uhdwccvQNLR3LXLNetgg433_pCyYyVsAswKdyczFeOGFM0tNqRKRF8hKndOdY9zFEC8GhYKd4JrYVCLOsDy0]--></g></svg>

<?xml version="1.0" encoding="us-ascii" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentStyleType="text/css" height="389px" preserveAspectRatio="none" style="width:570px;height:389px;background:#FFFFFF;" version="1.1" viewBox="0 0 570 389" width="570px" zoomAndPan="magnify"><defs/><g><rect fill="#FFFFFF" height="241.0938" style="stroke:#181818;stroke-width:1.0;" width="10" x="79" y="104.5625"/><rect fill="#FFFFFF" height="123.5313" style="stroke:#181818;stroke-width:1.0;" width="10" x="312" y="222.125"/><line style="stroke:#181818;stroke-width:0.5;stroke-dasharray:5.0,5.0;" x1="83.5" x2="83.5" y1="36.2969" y2="354.6563"/><line style="stroke:#181818;stroke-width:0.5;stroke-dasharray:5.0,5.0;" x1="316.5" x2="316.5" y1="190.3438" y2="354.6563"/><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="71" x="48.5" y="5"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="57" x="55.5" y="24.9951">RealCall</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="71" x="48.5" y="353.6563"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="57" x="55.5" y="373.6514">RealCall</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="169" x="232.5" y="353.6563"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="155" x="239.5" y="373.6514">RealInterceptorChain</text><rect fill="#FFFFFF" height="241.0938" style="stroke:#181818;stroke-width:1.0;" width="10" x="79" y="104.5625"/><rect fill="#FFFFFF" height="123.5313" style="stroke:#181818;stroke-width:1.0;" width="10" x="312" y="222.125"/><ellipse cx="7.5" cy="66.6797" fill="none" rx="4" ry="4" style="stroke:#181818;stroke-width:1.5;"/><polygon fill="#181818" points="72,63.4297,82,67.4297,72,71.4297,76,67.4297" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="12" x2="78" y1="67.4297" y2="67.4297"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="52" x="15" y="62.3638">execute</text><line style="stroke:#181818;stroke-width:1.0;" x1="84" x2="131" y1="91.5625" y2="91.5625"/><line style="stroke:#181818;stroke-width:1.0;" x1="131" x2="131" y1="91.5625" y2="104.5625"/><line style="stroke:#181818;stroke-width:1.0;" x1="90" x2="131" y1="104.5625" y2="104.5625"/><polygon fill="#181818" points="100,100.5625,90,104.5625,100,108.5625,96,104.5625" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="219" x="96" y="86.4966">getResponseWithInterceptorChain</text><line style="stroke:#181818;stroke-width:1.0;" x1="89" x2="131" y1="138.6953" y2="138.6953"/><line style="stroke:#181818;stroke-width:1.0;" x1="131" x2="131" y1="138.6953" y2="151.6953"/><line style="stroke:#181818;stroke-width:1.0;" x1="90" x2="131" y1="151.6953" y2="151.6953"/><polygon fill="#181818" points="100,147.6953,90,151.6953,100,155.6953,96,151.6953" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="122" x="96" y="133.6294">&#21019;&#24314;Interceptor List</text><polygon fill="#181818" points="220.5,176.8281,230.5,180.8281,220.5,184.8281,224.5,180.8281" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="89" x2="226.5" y1="180.8281" y2="180.8281"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="27" x="96" y="175.7622">new</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="169" x="232.5" y="159.6953"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="155" x="239.5" y="179.6904">RealInterceptorChain</text><polygon fill="#181818" points="300,218.125,310,222.125,300,226.125,304,222.125" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="89" x2="306" y1="222.125" y2="222.125"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="52" x="96" y="217.0591">proceed</text><line style="stroke:#181818;stroke-width:1.0;" x1="322" x2="364" y1="266.3906" y2="266.3906"/><line style="stroke:#181818;stroke-width:1.0;" x1="364" x2="364" y1="266.3906" y2="279.3906"/><line style="stroke:#181818;stroke-width:1.0;" x1="323" x2="364" y1="279.3906" y2="279.3906"/><polygon fill="#181818" points="333,275.3906,323,279.3906,333,283.3906,329,279.3906" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="141" x="329" y="246.1919">next = copy(index+1)</text><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="234" x="329" y="261.3247">&#22797;&#21046;&#24403;&#21069;&#36131;&#20219;&#38142;&#65292;&#25351;&#38024;&#25351;&#21521;&#19979;&#19968;&#20010;&#25318;&#25130;&#22120;</text><line style="stroke:#181818;stroke-width:1.0;" x1="322" x2="364" y1="323.6563" y2="323.6563"/><line style="stroke:#181818;stroke-width:1.0;" x1="364" x2="364" y1="323.6563" y2="336.6563"/><line style="stroke:#181818;stroke-width:1.0;" x1="323" x2="364" y1="336.6563" y2="336.6563"/><polygon fill="#181818" points="333,332.6563,323,336.6563,333,340.6563,329,336.6563" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="96" x="329" y="303.4575">intercept(next)</text><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="65" x="329" y="318.5903">&#25191;&#34892;&#25318;&#25130;&#22120;</text><!--SRC=[YyxNjLC8IatCSKxCoL6mKaYjI4qkBKdbWWiXIsjhMocafvO4fHONvESLfuPdbcHuvfMa5YMd5fJa5pbd96RcOTTefV2qO_RJtRkGL2luP1QNSAaheAelAQfqp4jnoy_9oIyFBK1MhgK5D2KljHnDYrDHPafwAX4AdHEJCuXIbv-NbvfSGfI9EJd1gKLbgKLO5QC717f8aQ0I5Eu5HVd9gQafN5WLuD88Ch-YHC5M8Jc_e58ZCoybjKBRK3Ccx-cItgSTsvxkdVoqi_V5biLFTkz-ENdV-pqzpthQNqxg099F9qnyigFxoOw69pjMFUjUzgnZrTEPAqYrEnCcf05o1T3QPvtBNopiWPi70000]--></g></svg>

<p>okhttp的拦截器分为<strong>应用拦截器</strong>和<strong>网络拦截器</strong>，拦截器按照顺序执行，看下RealCall.getResponseWithInterceptorChain中拦截器的顺序定义：</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">internal</span> <span class="function"><span class="keyword">fun</span> <span class="title">getResponseWithInterceptorChain</span><span class="params">()</span></span>: Response &#123;</span><br><span class="line">  <span class="comment">// Build a full stack of interceptors.</span></span><br><span class="line">  <span class="keyword">val</span> interceptors = mutableListOf&lt;Interceptor&gt;()</span><br><span class="line">  <span class="comment">//1.添加用户注册的应用拦截器</span></span><br><span class="line">  interceptors += client.interceptors </span><br><span class="line">  <span class="comment">//2.添加重试重定向拦截器</span></span><br><span class="line">  interceptors += RetryAndFollowUpInterceptor(client)</span><br><span class="line">  <span class="comment">//3.添加桥接拦截器，用于用户数据和网络数据之间的相互转换</span></span><br><span class="line">  interceptors += BridgeInterceptor(client.cookieJar)</span><br><span class="line">  <span class="comment">//4.添加缓存拦截器</span></span><br><span class="line">  interceptors += CacheInterceptor(client.cache)</span><br><span class="line">  <span class="comment">//5.连接拦截器，负责跟服务器建立连接</span></span><br><span class="line">  interceptors += ConnectInterceptor</span><br><span class="line">  <span class="keyword">if</span> (!forWebSocket) &#123;</span><br><span class="line">    <span class="comment">//6.添加用户自定义网络拦截器</span></span><br><span class="line">    interceptors += client.networkInterceptors</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//7.服务请求连接器</span></span><br><span class="line">  interceptors += CallServerInterceptor(forWebSocket)</span><br><span class="line"></span><br><span class="line">  <span class="keyword">val</span> chain = RealInterceptorChain(</span><br><span class="line">    call = <span class="keyword">this</span>,</span><br><span class="line">    interceptors = interceptors,</span><br><span class="line">    index = <span class="number">0</span>,</span><br><span class="line">    exchange = <span class="literal">null</span>,</span><br><span class="line">    request = originalRequest,</span><br><span class="line">    connectTimeoutMillis = client.connectTimeoutMillis,</span><br><span class="line">    readTimeoutMillis = client.readTimeoutMillis,</span><br><span class="line">    writeTimeoutMillis = client.writeTimeoutMillis</span><br><span class="line">  )</span><br><span class="line">  <span class="comment">//后面代码省略</span></span><br></pre></td></tr></table></figure>

<h2 id="拦截器的处理时序"><a href="#拦截器的处理时序" class="headerlink" title="拦截器的处理时序"></a>拦截器的处理时序</h2><p>这个是简化版的连接器处理时序，连接器调用到下一个都是要经过<code>RealInterceptorChain.proceed</code>，这里面逻辑比较简单，就是找到下一个连接器并执行，这个细节时序图中没有体现。</p>
<?xml version="1.0" encoding="us-ascii" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentStyleType="text/css" height="822px" preserveAspectRatio="none" style="width:867px;height:822px;background:#FFFFFF;" version="1.1" viewBox="0 0 867 822" width="867px" zoomAndPan="magnify"><defs/><g><rect fill="#FFFFFF" height="14" style="stroke:#181818;stroke-width:1.0;" width="10" x="84" y="67.4297"/><rect fill="#FFFFFF" height="23" style="stroke:#181818;stroke-width:1.0;" width="10" x="84" y="754.9531"/><rect fill="#FFFFFF" height="95.3984" style="stroke:#181818;stroke-width:1.0;" width="10" x="199" y="81.4297"/><rect fill="#FFFFFF" height="101.5313" style="stroke:#181818;stroke-width:1.0;" width="10" x="199" y="653.4219"/><rect fill="#FFFFFF" height="71.2656" style="stroke:#181818;stroke-width:1.0;" width="10" x="325" y="176.8281"/><rect fill="#FFFFFF" height="78.2656" style="stroke:#181818;stroke-width:1.0;" width="10" x="325" y="575.1563"/><rect fill="#FFFFFF" height="56.1328" style="stroke:#181818;stroke-width:1.0;" width="10" x="442" y="248.0938"/><rect fill="#FFFFFF" height="56.1328" style="stroke:#181818;stroke-width:1.0;" width="10" x="442" y="519.0234"/><rect fill="#FFFFFF" height="101.5313" style="stroke:#181818;stroke-width:1.0;" width="10" x="544" y="304.2266"/><rect fill="#FFFFFF" height="14" style="stroke:#181818;stroke-width:1.0;" width="10" x="544" y="505.0234"/><rect fill="#FFFFFF" height="14" style="stroke:#181818;stroke-width:1.0;" width="10" x="672" y="405.7578"/><rect fill="#FFFFFF" height="14" style="stroke:#181818;stroke-width:1.0;" width="10" x="672" y="491.0234"/><rect fill="#FFFFFF" height="71.2656" style="stroke:#181818;stroke-width:1.0;" width="10" x="780" y="419.7578"/><rect fill="none" height="486.7266" style="stroke:#000000;stroke-width:1.5;" width="730" x="131" y="153.6953"/><line style="stroke:#181818;stroke-width:0.5;stroke-dasharray:5.0,5.0;" x1="89" x2="89" y1="36.2969" y2="786.9531"/><line style="stroke:#181818;stroke-width:0.5;stroke-dasharray:5.0,5.0;" x1="204" x2="204" y1="36.2969" y2="786.9531"/><line style="stroke:#181818;stroke-width:0.5;stroke-dasharray:5.0,5.0;" x1="330" x2="330" y1="36.2969" y2="786.9531"/><line style="stroke:#181818;stroke-width:0.5;stroke-dasharray:5.0,5.0;" x1="447" x2="447" y1="36.2969" y2="786.9531"/><line style="stroke:#181818;stroke-width:0.5;stroke-dasharray:5.0,5.0;" x1="549" x2="549" y1="36.2969" y2="786.9531"/><line style="stroke:#181818;stroke-width:0.5;stroke-dasharray:5.0,5.0;" x1="677" x2="677" y1="36.2969" y2="786.9531"/><line style="stroke:#181818;stroke-width:0.5;stroke-dasharray:5.0,5.0;" x1="785" x2="785" y1="36.2969" y2="786.9531"/><rect fill="#FFFFFF" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="84" x="47" y="5"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="70" x="54" y="24.9951">&#24212;&#29992;&#25318;&#25130;&#22120;</text><rect fill="#FFFFFF" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="84" x="47" y="785.9531"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="70" x="54" y="805.9482">&#24212;&#29992;&#25318;&#25130;&#22120;</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="126" x="141" y="5"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="112" x="148" y="24.9951">&#37325;&#35797;&#37325;&#23450;&#21521;&#25318;&#25130;&#22120;</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="126" x="141" y="785.9531"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="112" x="148" y="805.9482">&#37325;&#35797;&#37325;&#23450;&#21521;&#25318;&#25130;&#22120;</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="84" x="288" y="5"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="70" x="295" y="24.9951">&#26725;&#25509;&#25318;&#25130;&#22120;</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="84" x="288" y="785.9531"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="70" x="295" y="805.9482">&#26725;&#25509;&#25318;&#25130;&#22120;</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="84" x="405" y="5"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="70" x="412" y="24.9951">&#32531;&#23384;&#25318;&#25130;&#22120;</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="84" x="405" y="785.9531"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="70" x="412" y="805.9482">&#32531;&#23384;&#25318;&#25130;&#22120;</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="84" x="507" y="5"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="70" x="514" y="24.9951">&#36830;&#25509;&#25318;&#25130;&#22120;</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="84" x="507" y="785.9531"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="70" x="514" y="805.9482">&#36830;&#25509;&#25318;&#25130;&#22120;</text><rect fill="#FFFFFF" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="84" x="635" y="5"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="70" x="642" y="24.9951">&#32593;&#32476;&#25318;&#25130;&#22120;</text><rect fill="#FFFFFF" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="84" x="635" y="785.9531"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="70" x="642" y="805.9482">&#32593;&#32476;&#25318;&#25130;&#22120;</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="112" x="729" y="5"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="98" x="736" y="24.9951">&#26381;&#21153;&#35831;&#27714;&#25318;&#25130;&#22120;</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="112" x="729" y="785.9531"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="98" x="736" y="805.9482">&#26381;&#21153;&#35831;&#27714;&#25318;&#25130;&#22120;</text><rect fill="#FFFFFF" height="14" style="stroke:#181818;stroke-width:1.0;" width="10" x="84" y="67.4297"/><rect fill="#FFFFFF" height="23" style="stroke:#181818;stroke-width:1.0;" width="10" x="84" y="754.9531"/><rect fill="#FFFFFF" height="95.3984" style="stroke:#181818;stroke-width:1.0;" width="10" x="199" y="81.4297"/><rect fill="#FFFFFF" height="101.5313" style="stroke:#181818;stroke-width:1.0;" width="10" x="199" y="653.4219"/><rect fill="#FFFFFF" height="71.2656" style="stroke:#181818;stroke-width:1.0;" width="10" x="325" y="176.8281"/><rect fill="#FFFFFF" height="78.2656" style="stroke:#181818;stroke-width:1.0;" width="10" x="325" y="575.1563"/><rect fill="#FFFFFF" height="56.1328" style="stroke:#181818;stroke-width:1.0;" width="10" x="442" y="248.0938"/><rect fill="#FFFFFF" height="56.1328" style="stroke:#181818;stroke-width:1.0;" width="10" x="442" y="519.0234"/><rect fill="#FFFFFF" height="101.5313" style="stroke:#181818;stroke-width:1.0;" width="10" x="544" y="304.2266"/><rect fill="#FFFFFF" height="14" style="stroke:#181818;stroke-width:1.0;" width="10" x="544" y="505.0234"/><rect fill="#FFFFFF" height="14" style="stroke:#181818;stroke-width:1.0;" width="10" x="672" y="405.7578"/><rect fill="#FFFFFF" height="14" style="stroke:#181818;stroke-width:1.0;" width="10" x="672" y="491.0234"/><rect fill="#FFFFFF" height="71.2656" style="stroke:#181818;stroke-width:1.0;" width="10" x="780" y="419.7578"/><ellipse cx="7.5" cy="66.6797" fill="none" rx="4" ry="4" style="stroke:#181818;stroke-width:1.5;"/><polygon fill="#181818" points="72,63.4297,82,67.4297,72,71.4297,76,67.4297" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="12" x2="78" y1="67.4297" y2="67.4297"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="57" x="15" y="62.3638">intercept</text><polygon fill="#181818" points="187,77.4297,197,81.4297,187,85.4297,191,81.4297" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="89" x2="193" y1="81.4297" y2="81.4297"/><line style="stroke:#181818;stroke-width:1.0;" x1="209" x2="251" y1="125.6953" y2="125.6953"/><line style="stroke:#181818;stroke-width:1.0;" x1="251" x2="251" y1="125.6953" y2="138.6953"/><line style="stroke:#181818;stroke-width:1.0;" x1="210" x2="251" y1="138.6953" y2="138.6953"/><polygon fill="#181818" points="220,134.6953,210,138.6953,220,142.6953,216,138.6953" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="39" x="216" y="105.4966">&#21021;&#22987;&#21270;</text><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="102" x="216" y="120.6294">ExchangeFinder</text><path d="M131,153.6953 L344,153.6953 L344,160.8281 L334,170.8281 L131,170.8281 L131,153.6953 " fill="#EEEEEE" style="stroke:#000000;stroke-width:1.5;"/><rect fill="none" height="486.7266" style="stroke:#000000;stroke-width:1.5;" width="730" x="131" y="153.6953"/><text fill="#000000" font-family="sans-serif" font-size="13" font-weight="bold" lengthAdjust="spacing" textLength="168" x="146" y="166.7622">&#37325;&#35797;&#21644;&#37325;&#23450;&#21521;&#22330;&#26223;&#36816;&#34892;&#22810;&#27425;</text><polygon fill="#181818" points="313,172.8281,323,176.8281,313,180.8281,317,176.8281" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="204" x2="319" y1="176.8281" y2="176.8281"/><line style="stroke:#181818;stroke-width:1.0;" x1="335" x2="377" y1="221.0938" y2="221.0938"/><line style="stroke:#181818;stroke-width:1.0;" x1="377" x2="377" y1="221.0938" y2="234.0938"/><line style="stroke:#181818;stroke-width:1.0;" x1="336" x2="377" y1="234.0938" y2="234.0938"/><polygon fill="#181818" points="346,230.0938,336,234.0938,346,238.0938,342,234.0938" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="78" x="342" y="200.895">&#22788;&#29702;&#35831;&#27714;&#22836;&#65292;</text><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="93" x="342" y="216.0278">&#21253;&#25324;&#21152;&#36733;cookie</text><polygon fill="#181818" points="430,244.0938,440,248.0938,430,252.0938,434,248.0938" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="330" x2="436" y1="248.0938" y2="248.0938"/><line style="stroke:#181818;stroke-width:1.0;" x1="452" x2="494" y1="277.2266" y2="277.2266"/><line style="stroke:#181818;stroke-width:1.0;" x1="494" x2="494" y1="277.2266" y2="290.2266"/><line style="stroke:#181818;stroke-width:1.0;" x1="453" x2="494" y1="290.2266" y2="290.2266"/><polygon fill="#181818" points="463,286.2266,453,290.2266,463,294.2266,459,290.2266" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="78" x="459" y="272.1606">&#21152;&#36733;&#26412;&#22320;&#32531;&#23384;</text><polygon fill="#181818" points="532,300.2266,542,304.2266,532,308.2266,536,304.2266" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="447" x2="538" y1="304.2266" y2="304.2266"/><line style="stroke:#181818;stroke-width:1.0;" x1="554" x2="596" y1="378.7578" y2="378.7578"/><line style="stroke:#181818;stroke-width:1.0;" x1="596" x2="596" y1="378.7578" y2="391.7578"/><line style="stroke:#181818;stroke-width:1.0;" x1="555" x2="596" y1="391.7578" y2="391.7578"/><polygon fill="#181818" points="565,387.7578,555,391.7578,565,395.7578,561,391.7578" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="104" x="561" y="328.2935">&#32531;&#23384;&#27744;&#33719;&#21462;&#21487;&#29992;&#36830;</text><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="91" x="561" y="343.4263">&#25509;&#25110;&#26032;&#24314;&#36830;&#25509;&#65292;</text><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="102" x="561" y="358.5591">&#21019;&#24314;Exchange&#21644;</text><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="72" x="561" y="373.6919">HTTP&#35299;&#30721;&#22120;</text><polygon fill="#181818" points="660,401.7578,670,405.7578,660,409.7578,664,405.7578" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="549" x2="666" y1="405.7578" y2="405.7578"/><polygon fill="#181818" points="768,415.7578,778,419.7578,768,423.7578,772,419.7578" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="677" x2="774" y1="419.7578" y2="419.7578"/><line style="stroke:#181818;stroke-width:1.0;" x1="790" x2="832" y1="464.0234" y2="464.0234"/><line style="stroke:#181818;stroke-width:1.0;" x1="832" x2="832" y1="464.0234" y2="477.0234"/><line style="stroke:#181818;stroke-width:1.0;" x1="791" x2="832" y1="477.0234" y2="477.0234"/><polygon fill="#181818" points="801,473.0234,791,477.0234,801,481.0234,797,477.0234" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="52" x="797" y="443.8247">&#21457;&#36865;&#35831;&#27714;</text><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="52" x="797" y="458.9575">&#35299;&#26512;&#21709;&#24212;</text><polygon fill="#181818" points="693,487.0234,683,491.0234,693,495.0234,689,491.0234" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;stroke-dasharray:2.0,2.0;" x1="687" x2="784" y1="491.0234" y2="491.0234"/><polygon fill="#181818" points="565,501.0234,555,505.0234,565,509.0234,561,505.0234" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;stroke-dasharray:2.0,2.0;" x1="559" x2="676" y1="505.0234" y2="505.0234"/><polygon fill="#181818" points="463,515.0234,453,519.0234,463,523.0234,459,519.0234" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;stroke-dasharray:2.0,2.0;" x1="457" x2="548" y1="519.0234" y2="519.0234"/><line style="stroke:#181818;stroke-width:1.0;" x1="452" x2="494" y1="548.1563" y2="548.1563"/><line style="stroke:#181818;stroke-width:1.0;" x1="494" x2="494" y1="548.1563" y2="561.1563"/><line style="stroke:#181818;stroke-width:1.0;" x1="453" x2="494" y1="561.1563" y2="561.1563"/><polygon fill="#181818" points="463,557.1563,453,561.1563,463,565.1563,459,561.1563" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="52" x="459" y="543.0903">&#20445;&#23384;&#32531;&#23384;</text><polygon fill="#181818" points="346,571.1563,336,575.1563,346,579.1563,342,575.1563" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;stroke-dasharray:2.0,2.0;" x1="340" x2="446" y1="575.1563" y2="575.1563"/><line style="stroke:#181818;stroke-width:1.0;" x1="335" x2="377" y1="619.4219" y2="619.4219"/><line style="stroke:#181818;stroke-width:1.0;" x1="377" x2="377" y1="619.4219" y2="632.4219"/><line style="stroke:#181818;stroke-width:1.0;" x1="336" x2="377" y1="632.4219" y2="632.4219"/><polygon fill="#181818" points="346,628.4219,336,632.4219,346,636.4219,342,632.4219" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="67" x="342" y="599.2231">&#20445;&#23384;cookie</text><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="52" x="342" y="614.356">gzip&#35299;&#21387;</text><polygon fill="#181818" points="220,649.4219,210,653.4219,220,657.4219,216,653.4219" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;stroke-dasharray:2.0,2.0;" x1="214" x2="329" y1="653.4219" y2="653.4219"/><line style="stroke:#181818;stroke-width:1.0;" x1="209" x2="251" y1="727.9531" y2="727.9531"/><line style="stroke:#181818;stroke-width:1.0;" x1="251" x2="251" y1="727.9531" y2="740.9531"/><line style="stroke:#181818;stroke-width:1.0;" x1="210" x2="251" y1="740.9531" y2="740.9531"/><polygon fill="#181818" points="220,736.9531,210,740.9531,220,744.9531,216,740.9531" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="65" x="216" y="677.4888">&#22788;&#29702;&#24322;&#24120;&#21644;</text><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="65" x="216" y="692.6216">&#36820;&#22238;&#30721;&#20915;&#23450;</text><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="65" x="216" y="707.7544">&#26159;&#21542;&#37325;&#35797;&#12289;</text><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="39" x="216" y="722.8872">&#37325;&#23450;&#21521;</text><polygon fill="#181818" points="105,750.9531,95,754.9531,105,758.9531,101,754.9531" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;stroke-dasharray:2.0,2.0;" x1="99" x2="203" y1="754.9531" y2="754.9531"/><ellipse cx="7.5" cy="768.2031" fill="none" rx="4" ry="4" style="stroke:#181818;stroke-width:1.5;"/><polygon fill="#181818" points="24.5,764.9531,14.5,768.9531,24.5,772.9531,20.5,768.9531" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="22.5" x2="83" y1="768.9531" y2="768.9531"/><!--SRC=[jLHDIm9H5DtFhn1QYdz0ebtHiaMxPYCsw12y6MIYQ9LI9ZhgM2PzJABaHuGrHe7Y1_wOkczUg_v2Drz5p1ibY3QpEFUSS-zxzypJ8mb3ZQfwX1W169JTyWtDjMZc5ivl0WixSTLGf0qjj2JKWi5mG2M6aeWgkY5vgoXuFSepvnI_S7y1Lkb7BHGCIhEhSvJX06Ike9q3ioAJvTreF49YoefADfM454jes_gd58xDRpNO0thki8d5wYOqBcYdFhSthJTfeUaPLW157exME72jD7Dwz245ZQUtaIaJC0zfhWFP6XkFevgsfIf-Nkxe1ExEF3q5KEHXpwapjJjWTtdLJyScLV5C0YZombyT7sgiq8DY1OeERXbfClaWPYgqqeNXWEluWJEN27oj1lSWazNrzJNMldPhIVIKz1znSySbTsWBSVEPpClaD-G5SIjs7h9rlWFl3lrBipHujyNIwtwIupB14z2g1ITvJFeihryF-kkT_2aSBvChH6R5uazPvzOyqJA9xQawtWyKSf92DaM7VtiCfdySZ5BGxq-ZnYPbkAn-92tzY7HCwPa3LejRFEydPVBjCwUdpsCtdN93MmnvA-y0]--></g></svg>

<h2 id="同步和异步请求源码解析"><a href="#同步和异步请求源码解析" class="headerlink" title="同步和异步请求源码解析"></a>同步和异步请求源码解析</h2><h3 id="okhttp异步请求调用"><a href="#okhttp异步请求调用" class="headerlink" title="okhttp异步请求调用"></a>okhttp异步请求调用</h3><p>异步请求需要调用enqueue，如下：</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">val</span> client = OkHttpClient()</span><br><span class="line"><span class="function"><span class="keyword">fun</span> <span class="title">run</span><span class="params">()</span></span> &#123;</span><br><span class="line">  <span class="keyword">val</span> request = Request.Builder()</span><br><span class="line">      .url(<span class="string">&quot;http://publicobject.com/helloworld.txt&quot;</span>)</span><br><span class="line">      .build()</span><br><span class="line"></span><br><span class="line">  client.newCall(request).enqueue(<span class="keyword">object</span> : Callback &#123;</span><br><span class="line">    <span class="keyword">override</span> <span class="function"><span class="keyword">fun</span> <span class="title">onFailure</span><span class="params">(call: <span class="type">Call</span>, e: <span class="type">IOException</span>)</span></span> &#123;&#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">override</span> <span class="function"><span class="keyword">fun</span> <span class="title">onResponse</span><span class="params">(call: <span class="type">Call</span>, response: <span class="type">Response</span>)</span></span> &#123;&#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>异步请求源码解析：</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 准备被执行的请求队列</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">val</span> readyAsyncCalls = ArrayDeque&lt;AsyncCall&gt;()</span><br><span class="line"><span class="comment">// 正在执行的请求队列</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">val</span> runningAsyncCalls = ArrayDeque&lt;AsyncCall&gt;()</span><br><span class="line"></span><br><span class="line"><span class="keyword">internal</span> <span class="function"><span class="keyword">fun</span> <span class="title">enqueue</span><span class="params">(call: <span class="type">AsyncCall</span>)</span></span> &#123;</span><br><span class="line">  synchronized(<span class="keyword">this</span>) &#123;</span><br><span class="line">    <span class="comment">// 添加到准备执行队列中</span></span><br><span class="line">    readyAsyncCalls.add(call)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 每个AsyncCall都维护了一个计数器，这段代码的作用是找到相同host的请求，相同host的请求使用同一个计数器</span></span><br><span class="line">    <span class="comment">// 相同host的请求默认最大是5个，超过5个后，新的请求会等待前面的请求执行完成</span></span><br><span class="line">    <span class="keyword">if</span> (!call.call.forWebSocket) &#123;</span><br><span class="line">      <span class="keyword">val</span> existingCall = findExistingCallWithHost(call.host)</span><br><span class="line">      <span class="keyword">if</span> (existingCall != <span class="literal">null</span>) call.reuseCallsPerHostFrom(existingCall)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  promoteAndExecute()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span> <span class="function"><span class="keyword">fun</span> <span class="title">promoteAndExecute</span><span class="params">()</span></span>: <span class="built_in">Boolean</span> &#123;</span><br><span class="line">  <span class="keyword">this</span>.assertThreadDoesntHoldLock()</span><br><span class="line"></span><br><span class="line">  <span class="keyword">val</span> executableCalls = mutableListOf&lt;AsyncCall&gt;()</span><br><span class="line">  <span class="keyword">val</span> isRunning: <span class="built_in">Boolean</span></span><br><span class="line">  synchronized(<span class="keyword">this</span>) &#123;</span><br><span class="line">    <span class="comment">// 遍历准备执行的队列</span></span><br><span class="line">    <span class="keyword">val</span> i = readyAsyncCalls.iterator()</span><br><span class="line">    <span class="keyword">while</span> (i.hasNext()) &#123;</span><br><span class="line">      <span class="keyword">val</span> asyncCall = i.next()</span><br><span class="line"></span><br><span class="line">      <span class="comment">// 默认最大是64个并行请求</span></span><br><span class="line">      <span class="keyword">if</span> (runningAsyncCalls.size &gt;= <span class="keyword">this</span>.maxRequests) <span class="keyword">break</span></span><br><span class="line">      <span class="comment">// 同一个host默认最多5个并行请求</span></span><br><span class="line">      <span class="keyword">if</span> (asyncCall.callsPerHost.<span class="keyword">get</span>() &gt;= <span class="keyword">this</span>.maxRequestsPerHost) <span class="keyword">continue</span></span><br><span class="line">      <span class="comment">// 从准备执行队列中删除该任务</span></span><br><span class="line">      i.remove()</span><br><span class="line">      <span class="comment">// 任务计数器加一，用于限制相同host的最大并行请求个数</span></span><br><span class="line">      asyncCall.callsPerHost.incrementAndGet()</span><br><span class="line">      executableCalls.add(asyncCall)</span><br><span class="line">      <span class="comment">//添加到正在执行队列中</span></span><br><span class="line">      runningAsyncCalls.add(asyncCall)</span><br><span class="line">    &#125;</span><br><span class="line">    isRunning = runningCallsCount() &gt; <span class="number">0</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 把任务添加到线程池中，启动网络请求</span></span><br><span class="line">  <span class="keyword">for</span> (i <span class="keyword">in</span> <span class="number">0</span> until executableCalls.size) &#123;</span><br><span class="line">    <span class="keyword">val</span> asyncCall = executableCalls[i]</span><br><span class="line">    asyncCall.executeOn(executorService)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> isRunning</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 请求结束时会执行该函数</span></span><br><span class="line"><span class="keyword">internal</span> <span class="function"><span class="keyword">fun</span> <span class="title">finished</span><span class="params">(call: <span class="type">AsyncCall</span>)</span></span> &#123;</span><br><span class="line">  call.callsPerHost.decrementAndGet()</span><br><span class="line">  finished(runningAsyncCalls, call)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span> <span class="function"><span class="keyword">fun</span> <span class="type">&lt;T&gt;</span> <span class="title">finished</span><span class="params">(calls: <span class="type">Deque</span>&lt;<span class="type">T</span>&gt;, call: <span class="type">T</span>)</span></span> &#123;</span><br><span class="line">  <span class="keyword">val</span> idleCallback: Runnable?</span><br><span class="line">  synchronized(<span class="keyword">this</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (!calls.remove(call)) <span class="keyword">throw</span> AssertionError(<span class="string">&quot;Call wasn&#x27;t in-flight!&quot;</span>)</span><br><span class="line">    idleCallback = <span class="keyword">this</span>.idleCallback</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 再次执行promoteAndExecute，遍历队列任务并执行新的任务，因为超过最大并行任务限制后，</span></span><br><span class="line">  <span class="comment">// 有些任务正在排队，所以任务结束后需要再次检查执行队列运行新的任务</span></span><br><span class="line">  <span class="keyword">val</span> isRunning = promoteAndExecute()</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (!isRunning &amp;&amp; idleCallback != <span class="literal">null</span>) &#123;</span><br><span class="line">    idleCallback.run()</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="同步请求源码解析"><a href="#同步请求源码解析" class="headerlink" title="同步请求源码解析"></a>同步请求源码解析</h3><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">RealCall</span> &#123;</span><br><span class="line">  <span class="keyword">override</span> <span class="function"><span class="keyword">fun</span> <span class="title">execute</span><span class="params">()</span></span>: Response &#123;</span><br><span class="line">    check(executed.compareAndSet(<span class="literal">false</span>, <span class="literal">true</span>)) &#123; <span class="string">&quot;Already Executed&quot;</span> &#125;</span><br><span class="line"></span><br><span class="line">    timeout.enter()</span><br><span class="line">    callStart()</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      client.dispatcher.executed(<span class="keyword">this</span>)</span><br><span class="line">      <span class="comment">// 直接在当前线程执行网络请求</span></span><br><span class="line">      <span class="keyword">return</span> getResponseWithInterceptorChain()</span><br><span class="line">    &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">      client.dispatcher.finished(<span class="keyword">this</span>)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Dispacher</span> &#123;</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">val</span> runningSyncCalls = ArrayDeque&lt;RealCall&gt;()</span><br><span class="line">  <span class="meta">@Synchronized</span> <span class="keyword">internal</span> <span class="function"><span class="keyword">fun</span> <span class="title">executed</span><span class="params">(call: <span class="type">RealCall</span>)</span></span> &#123;</span><br><span class="line">    runningSyncCalls.add(call)</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">internal</span> <span class="function"><span class="keyword">fun</span> <span class="title">finished</span><span class="params">(call: <span class="type">RealCall</span>)</span></span> &#123;</span><br><span class="line">    finished(runningSyncCalls, call)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>从Dispatcher可以看出，同步请求单独放在runningSyncCalls队列中（和异步请求独立），因此同步请求的数量是没有限制的。</p>
<h2 id="使用指南"><a href="#使用指南" class="headerlink" title="使用指南"></a>使用指南</h2><p><strong>OkHttpClient</strong>在整个app中最好采用单例模式，所有的网路请求都使用一个<strong>OkHttpClient</strong>实例。这是因为每个<strong>OkHttpClient</strong>都会创建自己的<em>连接池</em>和<em>线程池</em>。如果每次请求都创建一个OkHttpClient就会浪费系统资源。</p>
<p>当所有请求都使用了同一个OkHttpClient后，如果某次网络请求需要特殊配置可以使用<code>OkHttpClient::newBuilder()</code>，这种方式创建的client会共享连接池、线程池以及全局配置。</p>
<p>参考<a target="_blank" rel="noopener external nofollow noreferrer" href="https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/">OkHttpClients Should Be Shared</a></p>
<h2 id="编译okhttp-4-x"><a href="#编译okhttp-4-x" class="headerlink" title="编译okhttp 4.x"></a>编译okhttp 4.x</h2><ul>
<li>下载<a target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/square/okhttp/tree/okhttp_4.10.x">okhttp-4.10.x源码</a>、<a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.oracle.com/cn/java/technologies/downloads/#java17">Java jdk17</a>、<a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.jetbrains.com/idea/">IntelliJ IDEA</a></li>
<li>使用<strong>IntelliJ IDEA</strong>打开okhttp项目根目录。</li>
<li>Gradle JVM需要设置为jdk17，位置在<strong>IntelliJ IDEA</strong>【File】-&gt;【Settings】-&gt;【Build,Execution,Deployment】-&gt;【Build Tools-&gt;Gradle】。</li>
<li>在IDEA中打开<code>AsynchronousGet.kt</code>文件，然后点击运行main函数即可。</li>
</ul>
<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h2><p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://square.github.io/okhttp/">OkHttp</a><br><a target="_blank" rel="noopener external nofollow noreferrer" href="https://cloud.tencent.com/developer/article/1834390">Android 网络优化-DNS优化</a> <a target="_blank" rel="noopener external nofollow noreferrer" href="https://blog.csdn.net/makenothing/article/details/53292335">SNI</a><br><strong><a target="_blank" rel="noopener external nofollow noreferrer" href="https://square.github.io/okhttp/features/interceptors/">Interceptors</a></strong>: 重要<br><a target="_blank" rel="noopener external nofollow noreferrer" href="https://juejin.cn/post/7043336794099679268">“深入交流“系列：Okhttp（二）拦截器的实现</a><br><a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.jianshu.com/p/3b8cf45a5be7">OkHttp3 Cache</a><br><a target="_blank" rel="noopener external nofollow noreferrer" href="https://blog.csdn.net/huwei2003/article/details/70139062">HTTP 304状态码的详细讲解</a><br><a target="_blank" rel="noopener external nofollow noreferrer" href="https://blog.csdn.net/rui0838/article/details/52727343">Cache-Control no-cache与max-age=0的区别</a><br><a target="_blank" rel="noopener external nofollow noreferrer" href="https://blog.csdn.net/u012375924/article/details/82806617">浅谈http中的Cache-Control</a><br><a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.jianshu.com/p/7cb9300c6d71">谈谈OKHttp的几道面试题</a><br>okhttp如何实现dns拦截？<br><a target="_blank" rel="noopener external nofollow noreferrer" href="https://help.aliyun.com/document_detail/435264.html?spm=a2c4g.435263.0.0.2b8414a9tDQUVe">Android端HTTPDNS+OkHttp接入指南</a></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/okhttp/" rel="tag"># okhttp</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/posts/be1f8948.html" rel="prev" title="Flutter点击事件处理机制">
                  <i class="fa fa-angle-left"></i> Flutter点击事件处理机制
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/posts/37107c2e.html" rel="next" title="okhttp详解系列四：缓存拦截器">
                  okhttp详解系列四：缓存拦截器 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Jason</span>
  </div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener external nofollow noreferrer" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener external nofollow noreferrer" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.24/fancybox/fancybox.umd.js" integrity="sha256-oyhjPiYRWGXaAt+ny/mTMWOnN1GBoZDUQnzzgC7FRI4=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>


  <script class="next-config" data-name="mermaid" type="application/json">{"enable":true,"theme":{"light":"default","dark":"dark"},"js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.5.0/mermaid.min.js","integrity":"sha256-K7oJiQlDulzl24ZUFOywuYme1JqBBvQzK6m8qHjt9Gk="}}</script>
  <script type="module" src="/js/zenuml-definition-074a43fa.js"></script>
  <script type="module" src="/js/mermaid-zenuml.esm.min.mjs"></script>
  <script src="/js/third-party/tags/mermaid.js"></script>


  <script src="/js/third-party/fancybox.js"></script>



  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>



</body>
</html>
